package com.project.nimsen.web.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.jdbc.utils.Page;
import com.project.jdbc.web.BaseMVCAction;
import com.project.nimsen.dto.ProfitDto;
import com.project.nimsen.service.IProfitService;
/**
 * 反利信息Control
 * */
@Controller
@RequestMapping(value ="/sys/pro")
public class ProfitControl extends BaseMVCAction {
	@Resource
	IProfitService service;
	@RequestMapping(value="/list")
	public String add(){
		return "WEB-INF/sys/pro/list";
	}
	//页面查询用户信息
	@RequestMapping(value="/search")
	@ResponseBody
	public Map<String, Object> search(){
		ProfitDto dto= new ProfitDto();
		Map<String, Object> map = initMap(dto);
		//查询参数设置
		dto.setName(getParameter("name"));
		dto.setCkb_dk(getParameter("ckb_dk"));
		try{
			List<Map<String, Object>> rows = service.findUserByDto(dto);
			if(CollectionUtils.isEmpty(rows)){
				map.put("success", true);
				map.put("rows", "");
				map.put("total", 0);
				return map;
			}
			map.put("success", true);
			map.put("rows", rows);
			map.put("total", dto.getPage().getTotalCount());
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	//初始化map
	private Map<String, Object> initMap(ProfitDto dto){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("success", false);
		map.put("total", 0);
		Integer pageIndex = getIntegerParameter("page");
		if(pageIndex!=null){
			Page page = new Page(pageIndex);
			page.setPageSize(getIntegerParameter("rows"));
			dto.setPage(page);
		}
		return map;
	}
	//查询单个用户的详细反利信息
	@RequestMapping(value="/searchPro")
	@ResponseBody
	public Map<String, Object> searchPro(){
		ProfitDto dto= new ProfitDto();
		Map<String, Object> map = initMap(dto);
		map.put("success", false);
		map.put("rows", "");
		String userid=getParameter("userid");
		if(StringUtils.isBlank(userid)){
			map.put("success", true);
			map.put("rows", "");
			map.put("total", 0);
			return map;
		}
		dto.setUserid(userid);
		dto.setBack_info(getParameter("back_info"));
		try{
			List<Map<String, Object>> rows = service.findProByDto(dto);
			if(CollectionUtils.isEmpty(rows)){
				map.put("success", true);
				map.put("rows", "");
				map.put("total", 0);
				return map;
			}
			map.put("success", true);
			map.put("rows", rows);
			map.put("total", dto.getPage().getTotalCount());
		}catch(Exception e){
			e.printStackTrace();
		}
		return map;
	}
	//查询单个用户的详细反利信息
	@RequestMapping(value="/dkmon")
	@ResponseBody
	public Map<String, Object> dkmon(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", false);
		String ids = getParameter("ids");
		if(StringUtils.isBlank(ids)||",".equals(ids)){
			map.put("message", "必须选择一条反利信息");
			return map;
		}
		try{
			if(service.updateDKByIds(ids)){
				map.put("success", true);
			}else{
				map.put("message", "打款失败");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return map;
	}
}
